#Zu Online LTB parser

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class zuOnline_ltb(Model3D):
    '''Replace 'zuOnline_ltb' with a suitable name of your choice. Try to follow
    the standard: GameName_FormatExt'''
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(zuOnline_ltb,self).__init__("XAC")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        self.meshType = -1
        
    def read_name(self):
        
        return self.inFile.read_string(self.inFile.read_short())
        
    def parse_bones(self, numBones):
        
        for i in range(numBones):
            boneName = self.read_name()
        
    def parse_frames(self):
        
        self.inFile.read_short()
        frameName = self.read_name()
        
        print frameName
        self.inFile.read_byte()
        self.inFile.read_short()
        
        frmTransform = self.inFile.read_float(16)   #transform matrx
        
        
    def parse_faces(self, meshName, numFaces):
        
        for i in range(numFaces):
            v1, v2, v3 = self.inFile.read_short(3)
            
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
        
    def parse_vertices(self, meshName, numVerts, vertType):

        if vertType == 2:
            for i in range(numVerts):
                
                vx, vy, vz = self.inFile.read_float(3)
                nx, ny, nz = self.inFile.read_float(3)
                tu, tv = self.inFile.read_float(2)
                self.inFile.read_float()
                self.create_vertex(meshName, i)
                self.add_coords(meshName, i, [vx, vy, vz])
        elif vertType == 1:
            for i in range(numVerts):
                
                vx, vy, vz = self.inFile.read_float(3)
                nx, ny, nz = self.inFile.read_float(3)
                tu, tv = self.inFile.read_float(2)
                self.create_vertex(meshName, i)
                self.add_coords(meshName, i, [vx, vy, vz])
        elif vertType == 3:
            
            for i in range(numVerts):
                vx, vy, vz = self.inFile.read_float(3)
                self.inFile.read_float(2)
                nx, ny, nz = self.inFile.read_float(3)
                tu, tv = self.inFile.read_float(2)
                self.create_vertex(meshName, i)
                self.add_coords(meshName, i, [vx, vy, vz])
        elif vertType == 4:
            
            for i in range(numVerts):
                vx, vy, vz = self.inFile.read_float(3)
                self.inFile.read_float(3)
                nx, ny, nz = self.inFile.read_float(3)
                tu, tv = self.inFile.read_float(2)
                self.create_vertex(meshName, i)
                self.add_coords(meshName, i, [vx, vy, vz])
                
    def parse_unk1(self):
        
        count = self.inFile.read_long()
        for i in range(count):
            self.inFile.read_float(3)
        self.inFile.seek(3, 1)
        print self.inFile.tell()
            
    def parse_meshes(self, numMeshes):
        
        for i in range(numMeshes):
            meshName = self.read_name()
            self.create_object(meshName)
            count = self.inFile.read_long()
            self.inFile.read_long(count)
            self.inFile.read_long(2)
            for j in range(count):
                self.inFile.read_long(6)
                self.inFile.read_byte()
                self.inFile.read_long()
                self.inFile.read_long()
                numVerts = self.inFile.read_long()
                numFaces = self.inFile.read_long()
                vertType = self.inFile.read_long()
                self.inFile.read_long(5)
                if vertType == 1:
                    self.inFile.read_long()
                elif vertType in [2,3,4]:
                    self.inFile.read_short()
                print vertType, meshName, numVerts, numFaces, self.inFile.tell()
                self.parse_vertices(meshName, numVerts, vertType)
                self.parse_faces(meshName, numFaces)
                print self.inFile.tell()
                if vertType == 1:
                    self.inFile.read_short()
                if vertType in [3, 4]:
                    self.parse_unk1()
            
    
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        magic = self.inFile.read_long()
        self.inFile.read_long(4)
        count = self.inFile.read_long()
        self.inFile.read_long(count)
        self.inFile.read_long(4)
        self.read_name()
        self.inFile.read_short()
        self.inFile.read_long()
        self.inFile.read_short()
        numMeshes = self.inFile.read_long()
        self.parse_meshes(numMeshes)
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = zuOnline_ltb(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "\x01\x00\x09\x00", "LTB", "Zu Online LTB"

if __name__ == '__main__':
    
    file1 = "aoyun_huoju.ltb"
    file2 = "aoyun_weapon_jx.ltb"
    file3 = "js_ys_Rweapon008b.ltb"
    file4 = "missionitem027.ltb"
    obj = read_file(file2)